using System;
using ProtoModels;
using UnityEngine;

public class RateSettings : ScriptableObjectSingleton<RateSettings>
{
	[Serializable]
	public class SettingsBeforeAsk
	{
		public int gamesPlayedBeforeAsk = 5;

		public int stagesPassedBeforeAsk = -1;

		public bool ShouldShowRate(AppRateDAO model)
		{
			return model.playerGamesSinceLastRate >= gamesPlayedBeforeAsk && model.stagesPassedSinceLastRate >= stagesPassedBeforeAsk;
		}
	}

	public const string RateFilename = "rd.bytes";

	private AppRateDAO model;

	public SettingsBeforeAsk askFirstTime;

	public SettingsBeforeAsk askAgain;

	public SettingsBeforeAsk askNotLike;

	public int maxAskTimes = -1;

	public bool ShouldShowRate()
	{
		if (model.timesRateShown > 0 && model.userRatedApp)
		{
			return false;
		}
		if (maxAskTimes > 0 && model.timesRateShown >= maxAskTimes)
		{
			return false;
		}
		SettingsBeforeAsk settingsBeforeAsk = ((model.timesRateShown != 0) ? askAgain : askFirstTime);
		if (model.timesRateShown > 0 && !model.isUserEnjoying)
		{
			settingsBeforeAsk = askNotLike;
		}
		return settingsBeforeAsk.ShouldShowRate(model);
	}

	public void OnRateDialogShown()
	{
		model.timesRateShown++;
		model.timestampWhenAskedForRate = DateTime.UtcNow.Ticks;
		model.playerGamesSinceLastRate = 0;
		model.stagesPassedSinceLastRate = 0;
		Save();
	}

	public void OnUserEnjoying(bool enjoying)
	{
		Debug.Log("User enjoying " + enjoying);
		model.isUserEnjoying = enjoying;
		Save();
	}

	public void OnUserRated()
	{
		Debug.Log("User rated");
		model.userRatedApp = true;
		Save();
	}

	public void OnGameStarted()
	{
		model.playerGamesSinceLastRate++;
		Save();
	}

	public void OnStagePassed()
	{
		model.stagesPassedSinceLastRate++;
		Save();
	}

	protected override void Init()
	{
		base.Init();
		if (!ProtoIO.LoadFromFileCloudSync<AppRateDAO>("rd.bytes", out model))
		{
			model = new AppRateDAO();
		}
	}

	public void ReloadModel()
	{
		if (!ProtoIO.LoadFromFileCloudSync<AppRateDAO>("rd.bytes", out model))
		{
			model = new AppRateDAO();
		}
	}

	public void Save()
	{
		ProtoIO.SaveToFileCloudSync("rd.bytes", model);
	}
}
